package com.example.myuniversity;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.university.utils.SqliteDatabaseManager;

public class MajorDetailsActivity extends Activity {
	private ListView listView;
	private ImageView imageFinish;
	private TextView txtTitle;
	private BaseAdapter adapter;
	private Cursor cursor;
	private String uName,uId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_major_details);
		uName=getIntent().getExtras().getString("SchoolName");
		uId=getIntent().getExtras().getString("SchoolId");
		//完成一些控件的找到工作
		init();
		txtTitle.setText(uName);
		cursor=SqliteDatabaseManager.getInstance(this).getDatabase().query("院校专业表", new String[]{"专业名称","录取数","最高分","最低分","平行志愿","征求志愿","服从志愿"},
				"院校代码=?", new String[]{uId}, null, null, null);
		adapter=new MyAdapter();
		listView.setAdapter(adapter);
		imageFinish.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void init() {
		listView = (ListView) findViewById(R.id.listview_major_details);
		txtTitle=(TextView) findViewById(R.id.txt_major_details_title);
		imageFinish=(ImageView) findViewById(R.id.image_major_details_finish);
	}

	

	class MyAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return cursor.getCount();
		}
		@Override
		public Object getItem(int position) {
			return position;
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			cursor.moveToPosition(position);
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = getLayoutInflater().inflate(R.layout.layout_major_details_listview_item, parent, false);
				viewHolder.mName = (TextView) convertView.findViewById(R.id.txt_details_majorname);
				viewHolder.mNumber = (TextView) convertView.findViewById(R.id.txt_details_number);
				viewHolder.majorMax = (TextView) convertView.findViewById(R.id.txt_details_max_grade);
				viewHolder.majorMin = (TextView) convertView.findViewById(R.id.txt_details_min_grade);
				viewHolder.pingxing=(TextView) convertView.findViewById(R.id.txt_pingxing);
				viewHolder.zhengqiu=(TextView) convertView.findViewById(R.id.txt_zhengqiu);
				viewHolder.fucong=(TextView) convertView.findViewById(R.id.txt_fucong);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
				if (viewHolder!=null) {
					viewHolder.mName.setText(cursor.getString(0));
					viewHolder.mNumber.setText(cursor.getString(1));
					viewHolder.majorMax.setText(cursor.getString(2));
					viewHolder.majorMin.setText(cursor.getString(3));
					viewHolder.pingxing.setText(cursor.getString(4));
					viewHolder.zhengqiu.setText(cursor.getString(5));
					viewHolder.fucong.setText(cursor.getString(6));
				}
			return convertView;
		}

	}

	static class ViewHolder {
		TextView mName, mNumber, majorMax, majorMin,pingxing,zhengqiu,fucong;
	}
	@Override
	protected void onDestroy() {
		if (cursor!=null) {
			cursor.close();
		}
		super.onDestroy();
	}
}
